const { app, BrowserWindow } = require('electron')
const nuxtServer = require('./nuxtServer')
const fs = require('fs')
const path = require('path')
const koaServer = require('./server')
const nuxtConfig = fs.existsSync(path.resolve(__dirname, 'nuxt.config.js')) ? require(path.resolve(__dirname, 'nuxt.config')) : require(path.resolve(__dirname, '../../nuxt.config'))
function createWindow() {
  // 创建浏览器窗口
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
    },
  })

  // 加载index.html文件
  if (nuxtConfig.target === 'server') {
    win.loadURL('http://127.0.0.1:7001')
  } else {
    win.loadURL('http://127.0.0.1:7002')
  }
}

app.whenReady().then(async () => {
  if (nuxtConfig.target === 'server') {
    await nuxtServer()
  }
  koaServer()
  createWindow()
})
